﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace AutoSort
{
    public partial class AutoSortService : ServiceBase
    {
        private Configuration Config;

        public AutoSortService()
        {
            InitializeComponent();
            /* Load the Configuration */
            Config = new Configuration();
        }
        public void Scan()
        {
            FileInfo[] files = ScanFolder();
            foreach (FileInfo file in files)
            {
                HandleFile(file);
            }
        }

        private void HandleFile(FileInfo file)
        {
            /* Compare filename to all items */
            foreach (Item item in Config.Items)
            {
                /* Do we have a match? */
                if (item.Expression.IsMatch(file.Name.ToLower()))
                {
                    /* Move file to TargetFolder */
                    MoveFile(file, item.TargetFolder);
                    /* Stop searching for Match*/
                    return;
                }
            }
        }

        private void MoveFile(FileInfo newFile, string targetFolder)
        {
            /* Test that the directory exists*/
            DirectoryInfo dir = new DirectoryInfo(Config.OutputFolder + @"\" + targetFolder);
            if (!dir.Exists)
            {
                /* It does not exist, create it */
                dir.Create();
            }

            /* Test again, to make sure it exists now */
            dir = new DirectoryInfo(Config.OutputFolder + @"\" + targetFolder);
            if (dir.Exists)
            {
                string existingFilePath = Config.OutputFolder + @"\" + targetFolder + @"\" + newFile.Name;
                /* Does the new file allready exist? */
                FileInfo existingFile = new FileInfo(existingFilePath);
                if (existingFile.Exists)
                {
                    /* Keep the biggest file */
                    if (newFile.Length > newFile.Length)
                    {
                        /* Dont move, delete the new file */
                        newFile.Delete();
                    }
                    else
                    {
                        /* delete the smaller file, move the new, bigger filer */
                        existingFile.Delete();
                        newFile.MoveTo(existingFilePath);
                    }

                }
                else
                {
                    /* Move the file */
                    newFile.MoveTo(existingFilePath);
                }
            }
        }

        protected override void OnStart(string[] args)
        {
            
        }

        protected override void OnStop()
        {
        }

        private FileInfo[] ScanFolder()
        {
            DirectoryInfo dir = new DirectoryInfo(Config.InputFolder);
            return dir.GetFiles("*");
        }
    }
}
